<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Profile extends MY_Controller {
        var $seizoen;
        var $url;
	function __construct(){
		parent::__construct();
		$this->load->model('seizoenmodel');
                $this->load->model('spelermodel');
                $seizoenen = $this->seizoenmodel->SelectActive();
                $seizoenen = $seizoenen[0];
                $this->seizoen = $seizoenen;
                if(false !== $url = $this->session->flashdata('url')){
                    $this->url = $url;
                    
                }
                else {
                    if($this->agent->is_referral()){
                            $this->url = $this->agent->referrer();
                    } else {
                        $this->url = base_url(); 
                    }
                }
	}
	
	
	public function index()
	{
            if(false == $this->session->userdata('user')){
            $data['url'] = $this->url;
            $this->LoadView('login', $data);
        } else {
            $this->SetMessage("U bent al aangemeld");
            $this->GoBack();
        }
        }


	
        
        public function logout(){
            if($this->IsAdmin() and false !== $admin = $this->input->get('admin')){
                $this->session->unset_userdata('admin');
            } elseif(false !== $admin = $this->input->get('admin') or $this->IsAdmin() == false) {
                $this->session->unset_userdata('user');
            }
            
            $this->GoBack();
        }
	public function admin()
	{
            If($this->IsAdmin() == false){
		$data['page'] = 'admin/login';
                $data['url'] = $this->url;
                
                $this->LoadView('admin/login',$data);
            } else {
                $this->SetMessage("U bent al aangemeld als beheerder");
                $this->GoBack();
            }
	}
        
        public function login(){
            $url = $this->input->get('url');
            if($this->input->post('admin') == 1){
                $wachtwoord = $this->input->post('wachtwoord');
                if(sha1($wachtwoord) == $this->config->item('password')){
                    $this->session->set_userdata('admin',1);
                    $this->SetMessage("U bent nu aangemeld als beheerder");
                    if(false !== $url){
                        redirect($url);
                    }
                    else {
                        redirect(base_url());
                    }
                }
                else{
                    $this->session->set_flashdata('url',$url);
                    $this->SetMessage('Foutief wachtwoord');
                    redirect(base_url().'profile/admin');
                }
            }
            elseif($this->input->post('user') == 1){
                
                $wachtwoord = $this->input->post('wachtwoord');
                $email = $this->input->post('email');
                $dbwachtwoord = $this->spelermodel->GetPassword($email);
                if($dbwachtwoord != null and sha1($wachtwoord) == $dbwachtwoord){
                    $speler = $this->spelermodel->SelectByEmail($email,$this->seizoen->Id);
                    $this->SetMessage("Welkom <b>".$speler[0]->Voornaam."<b>");
                    $this->session->set_userdata('user',$speler[0]->Id);
                    if(false !== $url){
                        redirect($url);
                    }
                    else {
                        redirect(base_url());
                    }
                    
                    
                }
                else{
                    $this->session->set_flashdata('url',$url);
                    $this->SetMessage("Login mislukt");
                    $this->GoBack();
                    
                }
            }
                else {
                    $this->GoBack();
                }
                //die(print_r($this->session->all_userdata()));
                
                
            }
                    
            
            
            
        
	
	
}